package com.sbtr.util;

import javax.servlet.http.Cookie;

public class CookieUtils {
	private CookieUtils() {}
	/**
	 * 根据cookiename查找cookie
	 * @param cookies
	 * @param cookiename
	 * @return
	 */
	public static Cookie getCookieByName(Cookie[] cookies , String cookiename){
		if( null != cookies &&  cookies.length > 0 ){
			for( Cookie cookie : cookies ){
				if( null != cookie && cookiename.equals(cookie.getName()) ) {
					return cookie;
				}
			}
		}
		return null;
	}
	/**
	 * 获取Cookie value
	 * @param cookies
	 * @param cookiename
	 * @return
	 */
	public static String getCookieValue( Cookie[] cookies , String cookiename ){
		Cookie cookie = getCookieByName(cookies, cookiename);
		String result = null;
		if( null != cookie && 0 != cookie.getMaxAge() ){
			result = cookie.getValue();
		}
		return result;
	}
}
